﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.BLL;

public partial class Member_Control_ChangeWindowsUser : MP.Core.Authentication.Control.AuthenBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnChange_Click(object sender, EventArgs e)
    {
        string lstusers = txtUser.Text.Trim();
        string[] users = lstusers.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        List<string> userold = new List<string>();
        List<string> usernew = new List<string>();

        for (int i = 0; i < users.Length; i++)
        {
            string[] item = users[i].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            if (item.Length == 2 && !string.IsNullOrEmpty(item[0]) && !string.IsNullOrEmpty(item[1]))
            {
                if (!userold.Contains(item[0]) && !usernew.Contains(item[1]) && !userold.Contains(item[1]))
                {
                    userold.Add(item[0]);
                    usernew.Add(item[1]);
                }
            }
        }

        if (userold.Count > 0 && usernew.Count > 0)
        {
            for (int i = 0; i < userold.Count; i++)
            {
                if (Membership.GetUser(usernew[i]) == null)
                {
                    AspnetUsersManager.CreateInstant().UpdateUser(userold[i], usernew[i]);
                    HrPositionManager.CreateInstant().UpdateUserId(userold[i], usernew[i]);
                    HrEmployeeManager.CreateInstant().UpdateWindowsUser(userold[i], usernew[i]);
                    HrEmployeeManager.CreateInstant().UpdateUserTable(userold[i], usernew[i]);
                }
            }
        }

        Response.Redirect(Request.RawUrl);
    }
}
